import { invoke } from '@tauri-apps/api/tauri';

export interface InvokeError {
    error: string;
}

export interface MethodInfo {
    methodName: string;
    clientStream: boolean;
    serverStream: boolean;
}

export interface ServiceInfo {
    serviceName: string;
    methodList: MethodInfo[];
}

export interface MethodWithServiceInfo {
    serviceName: string;
    method: MethodInfo;
}

//获取解析结果文件
export async function getResultFile(grpcApiId: string): Promise<string> {
    const cmd = 'plugin:min_app_grpc|get_file';
    console.log(`%c${cmd}`, 'color:#0f0;', grpcApiId);
    return invoke<string>(cmd, { fileName: grpcApiId });
}

//生成解析结果文件
export async function genResultFile(grpcApiId: string, rootPath: string, importPathList: string[]): Promise<void> {
    const outPath = await getResultFile(grpcApiId);

    const cmd = 'plugin:min_app_grpc|exec';
    const request = {
        args: ["parse", "--rootPath", rootPath, "--outPath", outPath],
    };
    for (const importPath of importPathList) {
        request.args.push("--importPath");
        request.args.push(importPath);
    }
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    const jsonStr = await invoke<string>(cmd, request);
    const obj = JSON.parse(jsonStr) as InvokeError;
    if (obj.error !== undefined) {
        throw (obj.error);
    }
}

//读取协议描述信息
export async function readProtoDesc(grpcApiId: string): Promise<ServiceInfo[]> {
    const outPath = await getResultFile(grpcApiId);

    const cmd = 'plugin:min_app_grpc|exec';
    const request = {
        args: ["desc", "--in", outPath],
    };
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    const jsonStr = await invoke<string>(cmd, request);
    const obj = JSON.parse(jsonStr) as InvokeError | ServiceInfo[];
    if (Array.isArray(obj)) {
        return obj;
    } else {
        throw (obj.error);
    }
}

//生成请求数据
export async function genReqData(grpcApiId: string, serviceName: string, methodName: string): Promise<string> {
    const outPath = await getResultFile(grpcApiId);

    const cmd = 'plugin:min_app_grpc|exec';
    const request = {
        args: ["genReq", "--in", outPath, "--service", serviceName, "--method", methodName],
    };
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    const jsonStr = await invoke<string>(cmd, request);
    const obj = JSON.parse(jsonStr) as InvokeError;
    if (obj.error !== undefined) {
        throw (obj.error);
    }
    return jsonStr;
}

//请求grpc
export async function callMethod(grpcApiId: string, serviceName: string, methodName: string, remoteAddr: string, secure: boolean, reqData: string): Promise<string> {
    const outPath = await getResultFile(grpcApiId);

    const cmd = 'plugin:min_app_grpc|exec_pipe';
    const request = {
        args: ["call", "--in", outPath, "--service", serviceName, "--method", methodName, "--addr", remoteAddr],
        pipeData: reqData,
    };
    if (secure) {
        request.args.push("--secure");
    }
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return await invoke<string>(cmd, request);
}